/**
 * 
 */
package com.sys.console.business.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.sys.console.business.SysRoleAuthorityService;
import com.sys.console.mapper.Sys_Role_AuthorityMapper;
import com.sys.console.pojo.MeunAuthorityTreeJsonDto;
import com.sys.console.pojo.Sys_Role_Authority_Key;
import com.sys.object.common.ObjResultCode;


/**
 * @author HS
 *
 */
@Component
public class SysRoleAuthorityServiceImpl implements SysRoleAuthorityService {

	@Autowired Sys_Role_AuthorityMapper sysRoleAuthorityMapper;
	
	@Override
	public int addSysRoleAuthority(Sys_Role_Authority_Key record)
			throws Exception {
		// TODO Auto-generated method stub
		return sysRoleAuthorityMapper.insert(record);
	}

	@Override
	public int deleteSysRoleAuthority(Sys_Role_Authority_Key record)
			throws Exception {
		// TODO Auto-generated method stub
		return sysRoleAuthorityMapper.deleteByPrimaryKey(record);
	}

	@Override
	public int saveSysRoleAuthorityByList(List<Sys_Role_Authority_Key> list) throws Exception {
		// TODO Auto-generated method stub
		return sysRoleAuthorityMapper.insertByList(list);
	}

	@Override
	public ArrayList<Sys_Role_Authority_Key> selectByRidAndMid(Sys_Role_Authority_Key key) throws Exception {
		// TODO Auto-generated method stub
		return sysRoleAuthorityMapper.selectByRidAndMid(key);
	}

	@Override
	public int saveSysRoleAuthorityByTree(MeunAuthorityTreeJsonDto dto)
			throws Exception {
		if (StringUtils.hasText(dto.getId())) {
			List<Sys_Role_Authority_Key> listkey = new ArrayList<Sys_Role_Authority_Key>();
			Map<String,Sys_Role_Authority_Key> map = new HashMap<String,Sys_Role_Authority_Key>();
			if(StringUtils.hasText(dto.getId())){
				int delcount = 0;
				String[] authority = dto.getId().split("#",-1); // list = 10:SYS_ADD,SYS_BROWSE,SYS_EDIT#11:SYS_EDIT#12:SYS_DELETE#25:SYS_DOWNLOAD
				for (String str:authority) {
					String[] authoritys = str.split("_", -1);
					//String r_m = authoritys[1]+"_"+dto.getRid();
					int mid = Integer.parseInt(authoritys[1]); // 模块ID
					Sys_Role_Authority_Key delRoleAuthority = new Sys_Role_Authority_Key();
					delRoleAuthority.setMid(mid);
					delRoleAuthority.setRid(Integer.parseInt(dto.getRid()));
					if(!map.containsKey(dto.getRid())){
						delcount+=this.deleteSysRoleAuthority(delRoleAuthority);
						map.put(dto.getRid(), delRoleAuthority);
					}
					// 删除原来模块权限
					if (StringUtils.hasText(authoritys[0])) {
						Sys_Role_Authority_Key roleAuthority = new Sys_Role_Authority_Key();
						Integer aid = Integer.parseInt(authoritys[0]);
						roleAuthority.setRid(Integer.parseInt(dto.getRid()));
						roleAuthority.setAid(aid);
						roleAuthority.setMid(Integer.parseInt(authoritys[1]));
						listkey.add(roleAuthority);
					}
				}
				int success = 0;
				if(listkey!=null &&listkey.size()>0){
					success = this.saveSysRoleAuthorityByList(listkey);
				}
				return success;
			}
		}
		return 0;
	}

}
